package com.cff.pm.handler;

import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.cff.pm.client.HttpSpider;
import com.cff.pm.common.Constant;
import com.cff.pm.util.DateUtil;

public class LogHandler {
	LoginHandler loginHandler;
	
	public LoginHandler getLoginHandler() {
		return loginHandler;
	}

	public void setLoginHandler(LoginHandler loginHandler) {
		this.loginHandler = loginHandler;
	}

	public boolean hasTodayLog() throws Exception{
		if(loginHandler == null) {
			throw new IllegalArgumentException("LoginHandler不能为空");
		}
		HttpSpider httpSpider = loginHandler.getHttpSpider();
		if(httpSpider == null) {
			throw new IllegalArgumentException("httpSpider不能为空");
		}
		String content = httpSpider.doGetWithJsonResult(Constant.LOG_TODAY);
		return hashLog(content);
	}
	
	public boolean hashLog(String content)throws Exception{
		Document doc = Jsoup.parse(content);
		Elements effortList = doc.select("#mainContent");
		if(effortList == null){
			throw new Exception("jsoup解析出错");
		}
		Element effort = effortList.get(0);
		Elements tbody = effort.select("tbody");
		if(tbody !=null && tbody.size() > 0){
			Elements tr = tbody.select("tr");
			if(tr == null){
				throw new Exception("找到了tbody，没找到tr，哪儿出现问题了？");
			}
			int consume = 0;
			for(Element item : tr){
				Element td = item.select(".c-consumed").first();
				System.out.println(td.html());
				consume += Integer.parseInt(td.html().trim());
			}
			
			if(consume > 0)
				return true;
			return false;
		}else{
			Elements tfoot = effort.select(".text pull-left");
			if(tfoot != null)return false;
			else{
				throw new Exception("没找到tbody，也没找到text pull-left，哪儿出现问题了？");
			}
		}
		
	}
	
	public boolean hasYestDayLog() throws Exception{
		if(loginHandler == null) {
			throw new IllegalArgumentException("LoginHandler不能为空");
		}
		HttpSpider httpSpider = loginHandler.getHttpSpider();
		if(httpSpider == null) {
			throw new IllegalArgumentException("httpSpider不能为空");
		}
		String content = httpSpider.doGetWithJsonResult(Constant.LOG_YESTTODAY);
		return hashLog(content);
	}
	
	public boolean hasSpecifyLog(String date) throws Exception{
		if(loginHandler == null) {
			throw new IllegalArgumentException("LoginHandler不能为空");
		}
		HttpSpider httpSpider = loginHandler.getHttpSpider();
		if(httpSpider == null) {
			throw new IllegalArgumentException("httpSpider不能为空");
		}
		String day = String.format(Constant.LOG_SPECIFIED_DAY, date);
		String content = httpSpider.doGetWithJsonResult(day);
		return hashLog(content);
	}
	
	public boolean hasSpecifyLog(Date date) throws Exception{
		if(loginHandler == null) {
			throw new IllegalArgumentException("LoginHandler不能为空");
		}
		HttpSpider httpSpider = loginHandler.getHttpSpider();
		if(httpSpider == null) {
			throw new IllegalArgumentException("httpSpider不能为空");
		}
		String dateStr = DateUtil.format(date, DateUtil.SimpleDatePattern);
		String day = String.format(Constant.LOG_SPECIFIED_DAY, dateStr );
		String content = httpSpider.doGetWithJsonResult(day);
		return hashLog(content);
	}
}
